package com.byd.modules.biz.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byd.common.exception.RRException;
import com.byd.modules.biz.dao.DimensionDao;
import com.byd.modules.biz.dao.TbFlagDao;
import com.byd.modules.biz.dao.UserDictDao;
import com.byd.modules.biz.entity.DimensionEntity;
import com.byd.modules.biz.entity.SuperviseREntity;
import com.byd.modules.biz.service.DimensionService;
import com.byd.modules.biz.service.ExcelService;
import com.byd.modules.biz.service.UserDictService;



@Service("dimensionService")
public class DimensionServiceImpl implements DimensionService {
	@Autowired
	private DimensionDao dimensionDao;
	
	@Autowired
    private ExcelService excelService;
	
	@Autowired
    private TbFlagDao tbFlagDao;
	
//	@Autowired
//    private UserDictDao userDictDao;
	@Autowired
	private UserDictService userDictService;
	
	@Override
	public DimensionEntity queryObject(Long id){
		return dimensionDao.queryObject(id);
	}
	
	@Override
	public List<DimensionEntity> queryList(Map<String, Object> map){
		return dimensionDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return dimensionDao.queryTotal(map);
	}
	
	@Override
    @Transactional
	public void save(DimensionEntity dimension){
	    DimensionEntity parentDim = this.dimensionDao.queryObject(dimension.getParentId());
	    if (null == dimension.getDescription()) {
	    	//保证description不为NULL
	    	dimension.setDescription("");
		}
	    dimension.setLevel(parentDim!=null?parentDim.getLevel()+1:1);
	    if (dimension.getLevel()==4) {
		    if (dimensionDao.queryUnique(dimension.getName())>0) {
                throw new RRException("维度词库已经存在此名称或描述的维度！");
		    }
		}
		dimensionDao.save(dimension);
		dimension.setLinkId(parentDim!=null?parentDim.getLinkId()+dimension.getId()+"_":"_"+dimension.getId()+"_");
		dimension.setLinkName(parentDim!=null?parentDim.getLinkName()+dimension.getName()+"_":"_"+dimension.getName()+"_");
		dimension.setParentName(parentDim!=null?parentDim.getName():"");
		dimensionDao.update(dimension);

		//userDictDao.saveWord(dimension.getName());
		userDictService.saveName(dimension.getName());
		if (StringUtils.isNotBlank(dimension.getDescription())) {
			userDictService.saveNickName(dimension.getDescription());
		}
		tbFlagDao.updateCount(1);
	}
	
	@Override
    @Transactional
	public void update(DimensionEntity dimension){
	    DimensionEntity parentDim = this.dimensionDao.queryObject(dimension.getParentId());
	    dimension.setLinkId(parentDim!=null?parentDim.getLinkId()+dimension.getId()+"_":"_"+dimension.getId()+"_");
        dimension.setLinkName(parentDim!=null?parentDim.getLinkName()+dimension.getName()+"_":"_"+dimension.getName()+"_");
        dimension.setLevel(parentDim!=null?parentDim.getLevel()+1:1);
        dimension.setParentName(parentDim!=null?parentDim.getName():"");
		dimensionDao.update(dimension);

		//userDictDao.saveWord(dimension.getName());
		userDictService.saveName(dimension.getName());
		if (StringUtils.isNotBlank(dimension.getDescription())) {
			userDictService.saveNickName(dimension.getDescription());
		}
		tbFlagDao.updateCount(1);
	}
	
	@Override
	public void delete(Long id){
		dimensionDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		dimensionDao.deleteBatch(ids);
	}

    /**
     * 重载方法
     * @param parentId
     * @return
     */
    @Override
    public List<DimensionEntity> queryListFilter(Long parentId) {
        return dimensionDao.queryListFilter(parentId);
    }

    /**
     * 重载方法
     * @param parentId
     * @param response
     */
    @Override
    public void export(Map query, HttpServletResponse response) {
        List<DimensionEntity> queryList = dimensionDao.queryListTree(query);
        for (DimensionEntity dimensionEntity : queryList) {
            dimensionEntity.setName(dim2Space(dimensionEntity.getLevel(),dimensionEntity.getName()));
        }
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=Dimension.xls");
        excelService.exportExcel(new ExportParams(), DimensionEntity.class, queryList, response);
    }
    
    public String dim2Space(int level,String name){
        String newName="";
        for (int i = 1; i < level; i++) {
            newName+="  ";
        }
       return newName+name; 
    }

	@Override
	public List<DimensionEntity> queryListFilterByName(String name) {
        return dimensionDao.queryListFilterByName(name);
	}

	@Override
	public void upDescBySuperviseSub(SuperviseREntity superviseSub) {
		if (StringUtils.isBlank(superviseSub.getDim_des())
			 || StringUtils.isBlank(superviseSub.getDim_id())) {
			return;
		}
		DimensionEntity dimension = dimensionDao.queryObject(superviseSub.getDim_id());
		if (dimension==null) {
			return;
		}
		if (dimension.getDescription()==null || dimension.getDescription().indexOf(superviseSub.getDim_des())==-1) {
			DimensionEntity dimensionUp = new DimensionEntity();
			dimensionUp.setId(dimension.getId());
			if (dimension.getDescription().trim().length()>0) {
				dimensionUp.setDescription(superviseSub.getDim_des());
			} else if (dimension.getDescription().trim().length()>0) {
				dimensionUp.setDescription(dimension.getDescription()+";"+superviseSub.getDim_des());
			} else {
				dimensionUp.setDescription(superviseSub.getDim_des());
			}
			dimensionDao.update(dimensionUp);
		}
	}
}
